package eteam.aps.service.impl;

import eteam.aps.common.component.LoginUserRedisService;
import eteam.aps.common.entity.ParentEntity;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.mapper.ProductCategoryMapper;
import eteam.aps.model.entity.ProductCategory;
import eteam.aps.model.vo.ProductCategoryTreeVo;
import eteam.aps.model.vo.ProductCategoryVo;
import eteam.aps.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 装备分类业务层
 *
 * @author WMT
 */
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private LoginUserRedisService loginUserRedisService;

    @Transactional
    @Override
    public int insert(ProductCategory productCategory) {
        productCategory.setfId(Helper.GetId());
        productCategory.setfState(ParentEntity.FSTATE_DRAFT);
        LocalDateTime now = LocalDateTime.now();
        productCategory.setfCreatorTime(now);
        productCategory.setfLastModifyTime(now);
        return productCategoryMapper.insert(productCategory);
    }

    @Transactional
    @Override
    public int delete(String fId) {
        return productCategoryMapper.deleteByFId(fId);
    }

    @Transactional
    @Override
    public int update(ProductCategory productCategory) {
        productCategory.setfLastModifyTime(LocalDateTime.now());
        return productCategoryMapper.updateOne(productCategory);
    }

    @Override
    public ProductCategoryVo selectDetails(String fId) {
        return productCategoryMapper.selectDetails(fId);
    }

    @Override
    public JsonPage<ProductCategoryVo> selectAll(Map<String, Object> map, int page, int pageSize) {
        map.put("fState",ParentEntity.FSTATE_DELETED);
        int totalCount = productCategoryMapper.selectCountAll(map);
        List<ProductCategoryVo> list = null;
        if (totalCount > 0){
            map.put("offset",(page-1) * pageSize);
            map.put("limit",pageSize);
            list = productCategoryMapper.selectAll(map);
        }
        return JsonPage.builder(page,pageSize,totalCount,list);
    }


    @Override
    public int selectCountEcName(String fEcName) {
        return productCategoryMapper.selectCountEcName(fEcName);
    }

    @Override
    public int selectCountEcCode(String fEcCode) {
        return productCategoryMapper.selectCountEcCode(fEcCode);
    }

    @Override
    public ProductCategory selectFEcByFId(String fId) {
        return productCategoryMapper.selectFEcByFId(fId);
    }

    @Override
    public int selectFState(String fId) {
        return productCategoryMapper.selectFState(fId);
    }

    @Override
    public List<ProductCategoryTreeVo> selectEcTree(int fPcAffProduct) {
        Map<String,Object> map = new HashMap<>(2);
        map.put("fState",ParentEntity.FSTATE_DELETED);
        map.put("fPcAffProduct",fPcAffProduct);
        return productCategoryMapper.selectEcTree(map);
    }
}
